//
//  AppDelegate.swift
//  微博项目
//
//  Created by mac on 16/1/11.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        print(UserAccount.loadUserAccount())
        setThemeColor()
        registerNotification()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultRootViewController()
        window?.makeKeyAndVisible()
//        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        return true
    }
    
    //注册通知
    private func registerNotification(){
    
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "choseRootVC:", name: WBSwitchRootVC, object: nil)
    }
    
    //通知的响应方法中选择是显示TabBarVC 还是 欢迎页面
    @objc private func choseRootVC(n : NSNotification){
    
    print(n)
//        if n.object != nil{
//        //欢迎界面
//            window?.rootViewController = WelcomeViewController()
//        }else{
//        
//        window?.rootViewController = MainViewController()
//        }
        
        window?.rootViewController = (n.object == nil ? MainViewController() : WelcomeViewController())
    }
    
    //移除通知
    deinit{
    
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    //根据用户是否登录 指定默认的根视图控制器
    private func defaultRootViewController() -> UIViewController{
    
//        if UserAccountViewModel().userLogin{
//        
//        return WelcomeViewController()
//        }
//        //用户没有登录
//    return MainViewController()
        //三目
        return UserAccountViewModel().userLogin ? WelcomeViewController() : MainViewController()
        
    }

    //需要尽早设置 必须在视图实例化之前进行设置
    private func setThemeColor(){
    
        //导航视图控制器的全局颜色
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
}

